import { _decorator, Component, Node, Material, MeshRenderer, Prefab, instantiate, math } from 'cc';
import { Bullet } from '../bullet/Bullet';

const { ccclass, property, executeInEditMode, requireComponent, menu } = _decorator;

import { EnemyType } from '../framework/Constant';
import { PlaneEnemy } from '../plane/planeEnemy';

/**
 * Predefined variables
 * Name = GameManager
 * DateTime = Thu Jan 06 2022 17:22:02 GMT+0800 (中国标准时间)
 * Author = Kesa
 * FileBasename = gameManager.ts
 * FileBasenameNoExtension = gameManager
 * URL = db://assets/Scripts/gameManager.ts
 * ManualUrl = https://docs.cocos.com/creator/3.3/manual/zh/
 *
 */

@ccclass('GameManager')
// 在 编辑器里运行脚本
// @executeInEditMode(true)
// 在 菜单里面
@menu('customScript/manager')

/**
 *      业务逻辑:
 *          1. 控制子弹发射 ( 开始触摸发射, 结束触摸停止发射 )
 *          2. 控制...
 */
export class GameManager extends Component {
    // 飞机 -------------------------------------------------
    // 玩家控制飞机
    @property(Node)
    public playerPlane: Node = null;

    // 敌方飞机
    // 敌机_TYPE_A
    @property(Prefab)
    public enemyPlane_TYPA_A: Prefab = null;

    // 敌机_TYPE_B
    @property(Prefab)
    public enemyPlane_TYPA_B: Prefab = null;

    // 敌机根节点
    @property(Node)
    public enemyRoot: Node = null;

    // 敌方飞机生成次数
    @property
    public createEnemyTime = 3;

    // 当前敌方飞机生成次数
    private _currentCreateEnemyTime = 0;

    // 敌方飞机组合间隔
    private _combinationInterval = 5;

    // 敌方飞机速度设定
    @property
    public speed_enemy_TYPE_A = 30;

    @property
    public speed_enemy_TYPE_B = 50;

    // 子弹 -------------------------------------------------
    // 子弹类型
    @property(Prefab)
    public bullet_01: Prefab = null;
    @property(Prefab)
    public bullet_02: Prefab = null;
    @property(Prefab)
    public bullet_03: Prefab = null;
    @property(Prefab)
    public bullet_04: Prefab = null;
    @property(Prefab)
    public bullet_05: Prefab = null;

    // 玩家是否处于射击状态
    private _player_isShooting: boolean = false;

    // 子弹速度
    @property
    public bulletSpeed = 1;

    // 子弹射击周期
    @property
    public shootTime = 0.3;
    // 当前设计周期临时变量
    private _currentShootTime = 0;

    // 子弹管理节点
    @property(Node)
    public bulletRoot: Node = null;

    start() {
        this._init();
    }

    // 初始化
    private _init() {
        // 玩家射击重置
        this._currentShootTime = this.shootTime;

        // 敌方飞机组合设置
        // 这里定义的是
        // 每 10 s 切换一次模式,
        // 一共切换 3 次
        this.schedule(this._changeEnemyMode, this._combinationInterval, this.createEnemyTime - 1);
    }

    update(deltaTime: number) {
        this._currentShootTime += deltaTime;

        if (this._player_isShooting && this._currentShootTime > this.shootTime) {
            this.createPlayerBullet();
            this._currentShootTime = 0;
        }
    }

    // 控制射击, 传入 true 则射击, 传入 false 则停止射击
    public isShooting(value: boolean) {
        this._player_isShooting = value;
    }

    // ---------------------------------------
    // -----玩家飞机---------------------------
    // ---------------------------------------

    // 子弹射击
    public createPlayerBullet() {
        // TODO:这里之后要实现子弹类型更换
        let _bullet_proto = null;

        const bullet = instantiate(this.bullet_01);

        // 设置子弹父节点
        bullet.setParent(this.bulletRoot);
        const pos = this.playerPlane.position;

        // 设置子弹初始坐标
        bullet.setPosition(pos.x, pos.y, pos.z);
        const bulletComp = bullet.getComponent(Bullet);

        // 设置子弹速度
        bulletComp.bulletSpeed = this.bulletSpeed;
    }

    // ---------------------------------------
    // -----敌方飞机---------------------------
    // ---------------------------------------

    // 改变敌方飞机组合
    private _changeEnemyMode() {
        // log一下当前波次
        console.log('enemy wave:\t' + this._currentCreateEnemyTime);
        this._currentCreateEnemyTime++;

        // 创建敌方飞机 -> 一波创建 3 个
        this.schedule(this.createEnemyPlane, 1, 2);
    }

    // 创建敌方飞机
    public createEnemyPlane() {
        // 随机生成敌机类型
        const whichEnemy = math.randomRangeInt(1, 3);
        let prefab: Prefab = null;
        let speed = 0;
        if (whichEnemy === EnemyType.TYPE_A) {
            prefab = this.enemyPlane_TYPA_A;
        }

        // 根据随机出来的敌机类型拿到设计好的 敌机prefab 和 速度
        switch (whichEnemy) {
            case EnemyType.TYPE_A:
                prefab = this.enemyPlane_TYPA_A;
                speed = this.speed_enemy_TYPE_A;
                break;
            case EnemyType.TYPE_B:
                prefab = this.enemyPlane_TYPA_B;
                speed = this.speed_enemy_TYPE_B;
                break;
        }

        console.log(`生成敌机类型:${EnemyType[whichEnemy]}`);

        // 生成对应敌机,
        const enemy = instantiate(prefab);
        enemy.setParent(this.enemyRoot);
        const enemyComp = enemy.getComponent(PlaneEnemy);

        // 初始化敌机速度
        enemyComp.show(speed);

        // 初始化敌机位置: 随机一个位置
        const randomPos_X = math.randomRangeInt(-20, 21);
        console.log('random - X -->', randomPos_X);
        enemy.setPosition(randomPos_X, -100, -50);
    }
    public createEnemyBullet() {}
}
